package com.rc.web.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.rc.common.constant.Code;
import com.rc.common.exception.BusinessException;
import com.rc.pojo.dto.knowLedgvideo.KnowLedgeVideoAddDTO;
import com.rc.pojo.dto.knowLedgvideo.KnowLedgeVideoUpdateDTO;
import com.rc.pojo.dto.knowLedgvideo.KnowledgeVideoPageQuery;
import com.rc.pojo.po.KnowledgeVideo;
import com.rc.pojo.vo.knowledgevideo.KnowledgeVideoPageVO;
import com.rc.pojo.vo.knowledgevideo.KnowledgeVideoVO;
import com.rc.web.converter.KnowledgeVideoConverter;
import com.rc.web.mapper.KnowLedgeVideoMapper;
import com.rc.web.service.KnowledgeVideoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 知识视频服务实现类
 * 提供知识视频相关的业务逻辑实现
 */
@Service
@RequiredArgsConstructor
public class KnowledgeVideoServiceImpl extends ServiceImpl<KnowLedgeVideoMapper, KnowledgeVideo> implements KnowledgeVideoService{
    // 知识视频转换器
    private final KnowledgeVideoConverter videoConverter;

    /**
     * 根据病虫害ID列表获取视频列表
     * @param illIds 病虫害ID列表
     * @return 知识视频VO列表
     */
    @Override
    public List<KnowledgeVideoVO> getListByIllIds(List<Long> illIds) {
        // 根据病虫害ID列表查询视频列表
        List<KnowledgeVideo> knowledgeVideos = mapper.selectListByIllIds(illIds);
        // 转换为VO列表并返回
        return knowledgeVideos.stream()
                .map(videoConverter::toVO)
                .toList();
    }

    /**
     * 分页查询知识视频
     * @param query 分页查询条件
     * @return 分页结果
     */
    @Override
    public Page<KnowledgeVideoPageVO> getPage(KnowledgeVideoPageQuery query) {
        return mapper.selectPage(query);
    }

    /**
     * 根据ID删除知识视频
     * @param id 视频ID
     */
    @Override
    public void deleteById(Long id) {
        boolean success = this.removeById(id);
        if (!success){
            throw  new BusinessException(Code.NO_SUCH_VIDEO);
        }
    }

    /**
     * 添加知识视频
     * @param dto 知识视频添加DTO
     */
    @Override
    public void add(KnowLedgeVideoAddDTO dto) {
        // 转换DTO为PO并保存
        KnowledgeVideo knowledgeVideo = videoConverter.addDTO2PO(dto);
        this.save(knowledgeVideo);
    }

    /**
     * 更新知识视频
     * @param dto 知识视频更新DTO
     */
    @Override
    public void updateOneById(KnowLedgeVideoUpdateDTO dto) {
        // 转换DTO为PO并更新
        KnowledgeVideo knowledgeVideo = videoConverter.updateDTO2PO(dto);
        this.updateById(knowledgeVideo);
    }

    /**
     * 根据ID获取知识视频详情
     * @param id 视频ID
     * @return 知识视频VO
     */
    @Override
    public KnowledgeVideoVO getOneById(Long id) {
        KnowledgeVideo knowledgeVideo = this.getById(id);
        return videoConverter.toVO(knowledgeVideo);
    }

    /**
     * 根据病虫害ID获取知识视频列表
     * @param illId 病虫害ID
     * @return 知识视频VO列表
     */
    @Override
    public List<KnowledgeVideoVO> getByIllId(Long illId) {
        return mapper.selectListByIllIds(List.of(illId)).stream()
                .map(videoConverter::toVO)
                .toList();
    }
}
